﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Model.Orders;
using MobileSales.Website.OrderService;
using MobileSales.Website.Infrastructure;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure.Messaging;
using MobileSales.Model.Exceptions;
using System.ServiceModel;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class OrdersController : ControllerBase
    {
        #region Index
        //
        // GET: /Orders/

        public ActionResult Index()
        {
            IEnumerable<Order> orders = null;
            try
            {
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orders = orderService.GetOrderList();
                orderService.Close();
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderList, MessageType.Error);
            }
            return View(orders);
        } 
        #endregion

        #region Details
        //
        // GET: /Orders/Details/5

        public ActionResult Details(int id)
        {
            OrderServiceClient orderService = new OrderServiceClient();
            orderService.AddValidationToken(this.Token);
            Order order = orderService.GetOrder(id);
            orderService.Close();
            if (order == null)
            {
                this.SetMessage(ErrorStrings.ErrorOrderStatus, MessageType.Error);
                return RedirectToAction("Index");
            }
            return View(order);
        } 
        #endregion

        #region Create
        //
        // GET: /Orders/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Orders/Create

        [HttpPost]
        public ActionResult Create(int orderStatusId, Order order)
        {
            try
            {
                order.Status = new OrderStatus { Id = orderStatusId };
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orderService.StoreOrder(order);
                orderService.Close();
                this.SetMessage(InfoStrings.OrderAdded, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderAdding, MessageType.Error);

            }
            return View(order);
        } 
        #endregion

        #region Edit
        //
        // GET: /Orders/Edit/5

        public ActionResult Edit(int id)
        {
            return Details(id);
        }

        //
        // POST: /Orders/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, int orderStatusId, Order order)
        {
            try
            {
                order.Id = id;
                order.Status = new OrderStatus { Id = orderStatusId };
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orderService.StoreOrder(order);
                orderService.Close();
                this.SetMessage(InfoStrings.OrderUpdated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderUpdating, MessageType.Error);

            }
            return View(order);
        } 
        #endregion

        #region Delete
        //
        // GET: /Orders/Delete/5

        public ActionResult Delete(int id)
        {
            return Details(id);
        }

        //
        // POST: /Orders/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orderService.DeleteOrder(id);
                orderService.Close();
                this.SetMessage(InfoStrings.OrderDeleted, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderDeleting, MessageType.Error);

            }
            return RedirectToAction("Index");
        } 
        #endregion

        #region AddItem
        //
        // GET: /Orders/Create

        public ActionResult AddItem(int orderId)
        {
            return View(new OrderItem { OrderId = orderId });
        }

        //
        // POST: /Orders/Create

        [HttpPost]
        public ActionResult AddItem(int productId, OrderItem item)
        {
            try
            {
                item.Product = new MobileSales.Model.Products.Product { Id = productId };
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orderService.StoreOrderItem(item);
                orderService.Close();
                this.SetMessage(InfoStrings.OrderItemAdded, MessageType.Info);
                return RedirectToAction("Details", new { id = item.OrderId });
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderItemAdding, MessageType.Error);

            }
            return View(item);
        } 
        #endregion

        #region EditItem
        //
        // GET: /Orders/Create

        public ActionResult EditItem(int id)
        {
            OrderServiceClient orderService = new OrderServiceClient();
            orderService.AddValidationToken(this.Token);
            OrderItem orderItem = orderService.GetOrderItem(id);
            orderService.Close();
            if (orderItem == null)
            {
                this.SetMessage(ErrorStrings.ErrorOrderItem, MessageType.Error);
                return RedirectToAction("Index");
            }
            return View(orderItem);
        }

        //
        // POST: /Orders/Create

        [HttpPost]
        public ActionResult EditItem(int id, int productId, OrderItem item)
        {
            try
            {
                item.Id = id;
                item.Product = new Model.Products.Product { Id = productId };
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orderService.StoreOrderItem(item);
                orderService.Close();
                this.SetMessage(InfoStrings.OrderItemUpdated, MessageType.Info);
                return RedirectToAction("Details", new { id = item.OrderId });
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderItemUpdating, MessageType.Error);

            }
            return View(item);
        } 
        #endregion

        #region DeleteItem
        //
        // GET: /Orders/Create

        public ActionResult DeleteItem(int id)
        {
            return EditItem(id);
        }

        //
        // POST: /Orders/Create

        [HttpPost]
        public ActionResult DeleteItem(int id, int orderId, FormCollection collection)
        {
            try
            {
                OrderServiceClient orderService = new OrderServiceClient();
                orderService.AddValidationToken(this.Token);
                orderService.DeleteOrderItem(id);
                orderService.Close();
                this.SetMessage(InfoStrings.OrderItemDeleted, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorOrderItemDeleting, MessageType.Error);

            }
            return RedirectToAction("Details", new { id = orderId });
        } 
        #endregion

    }
}
